package aria.my12306;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import aria.my12306.aria.my12306.myFragment.MyFragment;
import aria.my12306.aria.my12306.myFragment.OrderFragment;
import aria.my12306.aria.my12306.myFragment.TicketFragment;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        viewPager = (ViewPager)findViewById(R.id.pager);
        viewPager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        actionBar.addTab(actionBar.newTab().setText("车票")
                .setTabListener(new MyTabListener()));
        actionBar.addTab(actionBar.newTab().setText("订单")
                .setTabListener(new MyTabListener()));
        actionBar.addTab(actionBar.newTab().setText("我的")
                .setTabListener(new MyTabListener()));

/*        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setNavigationMode(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/



        /*TicketFragment ticketFragment = new TicketFragment();
        OrderFragment orderFragment = new OrderFragment();
        MyFragment myFragment = new MyFragment();*/

    }
    class TabFragmentPagerAdapter extends FragmentPagerAdapter{

        public TabFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment f = null;
            switch (position){
                case 0:
                    f = new TicketFragment();
                    break;
                case 1:
                    f = new OrderFragment();
                    break;
                case 2:
                    f = new MyFragment();
                    break;
            }

            return f;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
    class MyTabListener implements ActionBar.TabListener{

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }
    }

}
